from certificate_manager.models import *
from certificate_manager.serializers import DepartmentSerializer, UserSerializer, CertSerializer
from django.test import tag
from rest_framework import status
from rest_framework.renderers import JSONRenderer
from rest_framework.test import APITestCase, force_authenticate
import datetime

class DepartmentTests(APITestCase):
	def setUp(self):
		dep = Department.objects.create(name='yves')
		dep.save()
		
		User.objects.create_superuser(uid='1', name='test', password='123', department=dep)
		
		
	def test_create_department(self):
		url = '/departments/'
		data = {'name': '中北大学'}
		self.client.login(uid='1', password='123')
		response = self.client.post(url, data, format='json')

		self.assertEqual(response.status_code, status.HTTP_201_CREATED)
		self.assertEqual(Department.objects.count(), 2)
		self.assertEqual(Department.objects.get(pk=2).name, '中北大学')

	def test_update_department(self):
		Department.objects.create(name='中北大学').save()

		# 居然没修改了！
		url = '/departments/2/'
		data = { 'name': 'zbdx' }
			
		self.client.login(uid='1', password='123')
		response = self.client.put(url, data, format='json')
		self.assertEqual(response.status_code, status.HTTP_200_OK)
		self.assertEqual(Department.objects.count(), 2)
		self.assertEqual(Department.objects.get(pk=2).name, 'zbdx')

